package com.fyy.im.server;

import com.fyy.im.protocol.MessageCodecSharable;
import com.fyy.im.protocol.ProcotolFrameDecoder;
import com.fyy.im.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.io.StringWriter;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/11 15:41
 */
@Slf4j
public class ChatServer {

    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup workers = new NioEventLoopGroup();
        //声明业务处理器
        LoggingHandler LOGGIN_HANDLE = new LoggingHandler(LogLevel.DEBUG);
        MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
        LoginMessageHandler LOGIN_HANDLER = new LoginMessageHandler();
        ChatRequestMessageHandler CHAT_MSG = new ChatRequestMessageHandler();
        GroupChatRequestMessageHandler GROUP_CHAT_MSG = new GroupChatRequestMessageHandler();
        GroupCreateRequestMessageHandler GROUP_CREATE = new GroupCreateRequestMessageHandler();
        GroupJoinRequestMessageHandler GROUP_JOIN = new GroupJoinRequestMessageHandler();
        GroupMembersRequestMessageHandler GROUP_MEMBERS = new GroupMembersRequestMessageHandler();
        GroupQuitRequestMessageHandler GROUP_QUIT = new GroupQuitRequestMessageHandler();
        QuitHandler QUIT = new QuitHandler();

        try {
            ServerBootstrap server = new ServerBootstrap();
            server.channel(NioServerSocketChannel.class);
            server.group(boss,workers);
            server.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel sc) throws Exception {
                    ChannelPipeline line = sc.pipeline();
                    line.addLast(new ProcotolFrameDecoder());
                    line.addLast(LOGGIN_HANDLE);
                    line.addLast(MESSAGE_CODEC);
                    line.addLast(new IdleStateHandler(5,0,0));
                    line.addLast(new ChannelDuplexHandler(){
                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            IdleStateEvent event = (IdleStateEvent) evt;
                            if(event.state() == IdleState.READER_IDLE){
                                log.info("已经5s未收到客户端数据了！");
//                                ctx.channel().close();
                            }
                        }
                    });
                    //添加业务处理器
                    line.addLast(LOGIN_HANDLER); //登录业务处理
                    line.addLast(CHAT_MSG); //单发业务处理
                    line.addLast(GROUP_CHAT_MSG); //小组群发业务处理
                    line.addLast(GROUP_CREATE); //创建群聊业务处理
                    line.addLast(GROUP_JOIN); //加入群聊业务处理
                    line.addLast(GROUP_MEMBERS); //群成员查询业务处理
                    line.addLast(GROUP_QUIT); //退出群聊业务处理
                    line.addLast(QUIT); //退出,断开连接
                }
            });
            ChannelFuture future = server.bind(8080).sync();
            future.channel().closeFuture().sync();
        }catch (Exception e){
            log.error("服务器错误:{}",e.getMessage());
        }finally {
            boss.shutdownGracefully();
            workers.shutdownGracefully();
        }

    }

}
